package com.dingyx.ps.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.time.DateFormatUtils;


import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;


public class UpgPay {

    private String appId;
    private String appKey;
    private String authorization;
    private String timestamp;
    private String nonce;

    private UpgPay(){
        this.appId = "8a81c1be76676b4d017861e6feda01fc";
        this.appKey = "fc6f17457a8042eab355d75edbcb0df3";
        this.timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        this.nonce = IdUtil.simpleUUID();
    }

    public static void main(String[] args) throws Exception {
        UpgPay upgPay = new UpgPay();
        String payUrl = upgPay.payV1();
        System.out.println("支付链接：" + payUrl);
    }



    public String payV1() throws Exception {
        JSONObject json = new JSONObject();
        json.put("mid", "898102157320108"); // 商户号
        json.put("tid", "AA002702");    // 终端号
        json.put("totalAmount", "1");      // 支付总金额
        json.put("instMid", "H5DEFAULT");
        json.put("transType", "UPG_BUSINESS");
        json.put("inAcctNo", Base64.encodeBase64String("测试".getBytes(StandardCharsets.UTF_8)));
        json.put("inAcctName", Base64.encodeBase64String("测试".getBytes(StandardCharsets.UTF_8)));
        json.put("merOrderId", "11LX1121200"); // 商户订单号
        json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));    // 报文请求时间
        json.put("bankName", Base64.encodeBase64String("测试".getBytes(StandardCharsets.UTF_8)));
        json.put("bizType", "100003");
        json.put("chnlNo", "03080001000");
        json.put("termType", "PC");
        json.put("chnlType", "PC");

        //json.put("notifyUrl", "http://api.mall.dev.longdaoyun.com/front/payment/chinaumsGatewayPayment/notify");
        System.out.println("请求报文:\n"+json);
        //String signature = getOpenBodySigForNetpay(appId, appKey, timestamp, nonce, json.toString());

        String signature = getSignature(appId, appKey, timestamp, nonce, json.toString());

        String param = "authorization=OPEN-FORM-PARAM" + "&appId=" + appId + "&timestamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(json.toString(), "UTF-8") + "&signature=" + URLEncoder.encode(signature, "UTF-8");
        String url = "https://api-mop.chinaums.com/v1/netpay/upg/order";
        System.out.println("URL:\n" + url + "?" + param);
        return url + "?" + param;
    }


    static String getSignature(String appid, String appkey, String timestamp, String nonce, String body) throws Exception {
        byte[] data = body.getBytes("utf-8");
        InputStream is = new ByteArrayInputStream(data);
        String testSH = DigestUtils.sha256Hex(is);
        String s1 = appid+timestamp+nonce+testSH;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(appkey.getBytes("utf-8"),"HmacSHA256"));
        byte[] localSignature = mac.doFinal(s1.getBytes("utf-8"));
        String localSignatureStr = Base64.encodeBase64String(localSignature);
        return localSignatureStr;
    }


}
